package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.PromotionCodeDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by marco on 2017/8/2.
 */
@Repository
public class PromotionCodeRepositoryImpl implements PromotionCodeRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MerchantProductRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;


    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 查询推广码列表
     * @param promotionCodeDto
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<PromotionCodeDto> findPromotionCodes(PromotionCodeDto promotionCodeDto, PageResult pageResult) {
        SQLPageDoing<PromotionCodeDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from promotion_code pc where 1=1");
            sql.append(" and pc.mall_id = :mallId");
            params.put("mallId", promotionCodeDto.getMallId());
            String name = promotionCodeDto.getName();
            if(StringUtils.isNotEmpty(name)) {
                sql.append(" and pc.name like :name");
                params.put("name", "%" + name + "%");
            }
            String startDate = promotionCodeDto.getStartDate();
            if(StringUtils.isNotEmpty(startDate)) {
                sql.append(" and pc.date_created >= :startDate");
                params.put("startDate", startDate);
            }
            String endDate = promotionCodeDto.getEndDate();
            if(StringUtils.isNotEmpty(endDate)) {
                sql.append(" and pc.date_created <= :endDate");
                params.put("endDate", endDate);
            }
            sql.append(" order by pc.id desc");
            fields.append("pc.id,pc.mall_id mallId,pc.name,pc.remark,pc.promote_url promoteUrl,pc.date_created dateCreated, pc.code_default codeDefault");
        };
        return doing.go(pageResult, PromotionCodeDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     * 统计推广码的访问次数、访问人数
     * @param promotionCodeDto
     * @return
     */
    @Override
    public PromotionCodeDto visitPageStatistics(PromotionCodeDto promotionCodeDto) {
        SQLObjectDoing<PromotionCodeDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from wxapp_visit_page wvp where 1=1");
            sql.append(" and wvp.promotion_code_id = :promotionCodeId");
            params.put("promotionCodeId", promotionCodeDto.getId());
            String startDate = promotionCodeDto.getStartDate();
            if(StringUtils.isNotEmpty(startDate)) {
                sql.append(" and wvp.operate_date >= :startDate");
                params.put("startDate", startDate);
            }
            String endDate = promotionCodeDto.getEndDate();
            if(StringUtils.isNotEmpty(endDate)) {
                sql.append(" and wvp.operate_date <= :endDate");
                params.put("endDate", endDate);
            }
            fields.append("sum(wvp.page_visit_pv) viewCount,sum(wvp.page_visit_uv) customerCount");
        };
        return doing.go(PromotionCodeDto.class, jdbcTemplate, log, objectMapper);
    }

    /**
     * 统计推广码的售出数、核销数
     * @param promotionCodeDto
     * @param promotionCodeDto
     * @return
     */
    @Override
    public PromotionCodeDto promotionCodeStatistics(PromotionCodeDto promotionCodeDto) {
        SQLObjectDoing<PromotionCodeDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM merchant_order_item moi left join mall_order mo on mo.order_num=moi.mall_order_num");
            sql.append(" where exists(select 1 from order_event oe where moi.merchant_order_num=oe.merchant_order_num and oe.event_type='PAYED')");
            sql.append(" and moi.mall_id = :mallId");
            sql.append(" and mo.mall_id = :mallId");
            params.put("mallId", promotionCodeDto.getMallId());
            sql.append(" and mo.promotion_code_id = :promotionCodeId");
            params.put("promotionCodeId", promotionCodeDto.getId());
            String startDate = promotionCodeDto.getStartDate();
            if(StringUtils.isNotEmpty(startDate)) {
                sql.append(" and moi.date_created >= :startDate");
                params.put("startDate", startDate);
            }
            String endDate = promotionCodeDto.getEndDate();
            if(StringUtils.isNotEmpty(endDate)) {
                sql.append(" and moi.date_created <= :endDate");
                params.put("endDate", endDate);
            }
            fields.append("sum(moi.buy_count) viewCount,sum(moi.verify_count) verifyCount");
        };
        return doing.go(PromotionCodeDto.class, jdbcTemplate, log, objectMapper);
    }
}
